import Vue from 'vue';
import Vuex from 'vuex';
import { getEquipListApi, getMusicListApi, getisBtnTopApi } from '@/api/index';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    username: localStorage.getItem('username'),
    equipList: [], //获取音响设备列表
    systemMusic: {}, //获取系统音乐库
    layoutConfig: {}, //主页面布局配置
    isCollapse: false //是否折叠
  },
  mutations: {
    CHANGECOLLAPSE(_state, value) {
      this.isCollapse = value;
    },
    CHANGESYSTEMUSICS(state, value) {
      state.systemMusic = value;
      // console.log('系统音乐库', state.systemMusic);
    },
    GETUSERNAME(state, value) {
      state.username = value;
    },
    GETEQUIPLIST(state, value) {
      state.equipList = value;
    }
  },
  actions: {
    /**
     * @use 获取音响设备列表
     */
    async getEquipList(store, item) {
      try {
        const res = await getEquipListApi();
        if (res.code === 0) {
          const equipList = res.data.map((item) => {
            return {
              isChecked: false,
              ...item
            };
          });
          store.commit('GETEQUIPLIST', equipList);
        }

       
      } catch (e) {
        console.log(e);
      }
    },

    /**
     * @use 获取系统音乐库
     */
    async getSystemMusicList(store) {
      try {
        const res = await getMusicListApi();
        if (res.code === 0) {
          const list = res.data;
          const key = Object.keys(list);
          // console.log(key);
          const newSystemMusic = key.map((item) => {
            // console.log(item);
            return {
              name: item,
              list: list[item],
              isChecked: false
            };
          });
          store.commit('CHANGESYSTEMUSICS', newSystemMusic);
        }
      } catch (e) {
        console.log(e);
      }
    },

    /**
     *
     * @use 菜单是否折叠
     */
    changeCollapse(store, value) {
      store.commit('CHANGECOLLAPSE', value);
    },

    getUserName(store, value) {
      // localStorage.setItem('username', value);
      store.commit('GETUSERNAME', value);
    }
  }
});
